package com.emuporium.gymrat.server.routine;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emuporium.gymrat.server.data.RoutineDB;

@SuppressWarnings("serial")
public class RoutineAddEntrySubmitServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int routineId = new Integer(request.getParameter("id"));
		int exerciseId = new Integer(request.getParameter("exer"));
		String variationId[] = request.getParameterValues("variation");
		int order = new Integer(request.getParameter("order"));
		int sets = new Integer(request.getParameter("sets"));
		int reps = new Integer(request.getParameter("reps"));
		String notes = request.getParameter("notes");
		ArrayList<String> vars = new ArrayList<String>();
		
		if (variationId != null)
			vars.addAll(Arrays.asList(variationId));
		RoutineDB.insertEntry(routineId, exerciseId, order, sets, reps, vars,
				notes);
		
		request.setAttribute("redirecturl", "/routine/modify?id=" + routineId);
		String url = "/includes/redirect.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}
}
